//create by lwy 2017年9月28日15:44:48
var shopId = '';
var shopgradedValue = '';
$(function () {
    doAjax();
})

function doAjax() {
    $.ajax({
        url:'/zuoDanC/getGoodsId.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            zuoDanId:GetQueryString('zuoDanId'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                var goodsData = data.datas.goodsIdList;
                shopId = data.datas.shopId;
                for(var i=0;i<goodsData.length;i++){
                    $('<div class="goods_comment" goodsId="'+goodsData[i].goodsId+'" orderId="'+goodsData[i].orderId+'">\n' +
                        '<div class="goods_title mui-clearfix">\n' +
                        '<div class="small_img"><img src="'+goodsData[i].picture+'" alt=""></div>\n' +
                        '<div class="grade_text">宝贝评分</div>\n' +
                        '<div class="goods_grade">\n' +
                        '<img src="../img/doComment_star.png" alt="1" goodsStar="true" goodsText="不满意">\n' +
                        '<img src="../img/doComment_star.png" alt="2" goodsStar="true" goodsText="不满意">\n' +
                        '<img src="../img/doComment_star.png" alt="3" goodsStar="true" goodsText="一般">\n' +
                        '<img src="../img/doComment_star.png" alt="4" goodsStar="true" goodsText="好">\n' +
                        '<img src="../img/doComment_star.png" alt="5" goodsStar="true" goodsText="非常好">\n' +
                        '</div>\n' +
                        '<div class="grade_dec" goodsText="5">非常好</div>\n' +
                        '</div>\n' +
                        '<div class="goods_detail">\n' +
                        '<textarea placeholder="宝贝符合您的期待吗？说说您的使用感受吧"></textarea>\n' +
                        '</div>\n' +
                        '<div class="goods_img mui-clearfix" goodsId="'+goodsData[i].goodsId+'" goodsImg="">\n' +
                        '<img src="../img/add_photo.jpg" alt="添加图片">\n' +
                        '<img src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<img src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        // '<img src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        // '<img src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<label class="file">' +
                        '<input class="file_input" id="'+goodsData[i].goodsId+'" name="file" type="file" accept="image/*;capture=camera;">' +
                        '</label>' +
                        '</div>\n' +
                        '</div>\n' +
                        '<div class="line"></div>').appendTo($('.goods_comment_box'));
                }
                $('<div class="shop_comment">\n' +
                    '<div class="shop_title mui-clearfix">\n' +
                    '<div class="small_img"><img class="shop_icon" src="../img/shop_icon2.jpg" alt=""></div>\n' +
                    '<div class="grade_text">店铺评分</div>\n' +
                    '<div class="shop_grade">\n' +
                    '<img src="../img/doComment_star.png" alt="1" shopStar="true" shopText="不满意">\n' +
                    '<img src="../img/doComment_star.png" alt="2" shopStar="true" shopText="不满意">\n' +
                    '<img src="../img/doComment_star.png" alt="3" shopStar="true" shopText="一般">\n' +
                    '<img src="../img/doComment_star.png" alt="4" shopStar="true" shopText="好">\n' +
                    '<img src="../img/doComment_star.png" alt="5" shopStar="true" shopText="非常好">\n' +
                    '</div>\n' +
                    '<div class="grade_dec" id="shopText">非常好</div>\n' +
                    '</div>\n' +
                    '<div class="shop_detail">\n' +
                    '<textarea placeholder="说说您对店铺的感受吧" id="shopContent"></textarea>\n' +
                    '</div>\n' +
                    '<div class="shop_img mui-clearfix">\n' +
                    '<img id="photo1" src="../img/add_photo.jpg" alt="添加图片">\n' +
                    '<img id="photo2" src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                    // '<img id="photo3" src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                    // '<img id="photo4" src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                    '<img id="photo5" src="../img/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                    '<label class="file" onchange="changeImg()">' +
                    '<input id="file" name="file" type="file" accept="image/*;capture=camera;">' +
                    '</label>' +
                    '</div>\n' +
                    '</div>').appendTo($('.goods_comment_box'));
            }
        }
    })
}
var shopGrade = 5;
//店铺星级评分
$('body').on('click','.shop_grade img',function () {

    $('#shopText').html($(this).attr('shopText'));
    shopGrade = $(this).attr('alt');

    if($(this).attr('shopStar')=='true'){
        $(this).nextAll().attr({'shopStar':'false','src':'../img/doComment_star_h.png'})
    }else{
        $(this).attr({'shopStar':'true','src':'../img/doComment_star.png'})
        $(this).prevAll().attr({'shopStar':'true','src':'../img/doComment_star.png'})
    }
})

//商品星级评分
$('body').on('click','.goods_grade img',function () {
    var gradeHtml = $(this).attr('goodsText');
    var goodsStar = $(this).attr('alt');
    $(this).parent().siblings('.grade_dec').html(gradeHtml);
    $(this).parent().siblings('.grade_dec').attr('goodsText',goodsStar);
    if($(this).attr('goodsStar')=='true'){
        $(this).nextAll().attr({'goodsStar':'false','src':'../img/doComment_star_h.png'})
    }else{
        $(this).attr({'goodsStar':'true','src':'../img/doComment_star.png'})
        $(this).prevAll().attr({'shopStar':'true','src':'../img/doComment_star.png'})
    }
})

//添加评价
$('.set-certain').click(function () {
    var goodsComment = '';
    $('.goods_comment').each(function () {
        var orderId = $(this).attr('orderId');
        var goodsId = $(this).attr('goodsid');
        var goodsGrade = $(this).children('.goods_title').children('.grade_dec').attr('goodstext');
        var comments = $(this).children('.goods_detail').children().val();
        if(comments==""){
            comments="好评！"
        }
        var goodsImg = $(this).children('.goods_img').attr('goodsImg');
        goodsComment+='orderId='+orderId+';goodsId='+goodsId+';graded='+goodsGrade+';comments='+comments+';mig='+goodsImg+'#'
        //goodsComment+='orderId='+orderId+'$'+goodsId+'$'+goodsGrade+'$'+comments+'$'+goodsImg+'#'
    })
    $.ajax({
        url:'/commentsC/addComments.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            shopId:shopId,
            content:$('#shopContent').val(),
            zuodanId:GetQueryString('zuoDanId'),
            imgUrl:imgs, //店铺图片
            gradedValue:shopGrade, //店铺评分值
            goodsCommentss:goodsComment,
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('评价成功',function () {
                    window.location.href="userOrder_v2.html?zuoDanStateValue=2"
                })
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})

//店铺上传图片
var imgs="";
var IMG=0;
function changeImg(){
    if(IMG>4){
        mui.alert('图片已上传完毕！');
        return;
    }else{
        selectOver();
    }
}
function selectOver(){
    IMG++;
    $.ajaxFileUpload({
        url:'/uploadImage.do',
        type:'POST',
        fileElementId:'file',
        dataType: 'JSON',
        success: function (data) {
            var results=data.split('|');
            var flag=results[0].split('>');
            if(flag[1] != 'success'){
                mui.alert('上传失败');
                IMG--;
                return;
            }
            var img=results[1].substring(results[1].length-6,0);
            IMG++;
            if($('#photo1').attr('src')=='../img/add_photo.jpg'){
                $("#photo1").attr('src',img);
                imgs=imgs+$("#photo1").attr("src");
                $("#photo2").attr("src","../img/add_photo.jpg");
                $("#photo2").css('display','block');
            }else if($('#photo2').attr('src')=='../img/add_photo.jpg'){
                $("#photo2").attr('src',img);
                imgs=imgs+","+$("#photo2").attr("src");
                $("#photo5").attr("src","../img/add_photo.jpg");
                $("#photo5").css('display','block');
            }else if($("#photo5").attr("src")==""||$('#photo5').attr('src')=='../img/add_photo.jpg'){
                $("#photo5").attr('src',img);
                imgs=imgs+","+$("#photo5").attr("src");
            }

        },
        error:function(data){
            IMG--;
            mui.alert('上传失败');

        }
    })
}

//商品上传图片
$('body').on('change','.file_input',function () {
    var loadId = $(this).attr('id');
    changeImg2(loadId);
})



function changeImg2(a){
    var imgs2="";
    var IMG2=0;
    if(IMG2>4){
        mui.alert('图片已上传完毕！');
        return;
    }else{
        selectOver2(a);
    }

    function selectOver2(a){
        IMG2++;
        $.ajaxFileUpload({
            url:'/uploadImage.do',
            type:'POST',
            fileElementId:a,
            dataType: 'JSON',
            success: function (data) {
                var results2=data.split('|');
                var flag2=results2[0].split('>');
                if(flag2[1] != 'success'){
                    mui.alert('上传失败');
                    IMG2--;
                    return;
                }

                var img2=results2[1].substring(results2[1].length-6,0);
                IMG2++;

                var imgBox = $('#'+a+'').parent().parent().find('img');
                if($(imgBox[0]).attr('src')=='../img/add_photo.jpg'){
                    $(imgBox[0]).attr('src',img2);
                    imgs2=imgs2+$(imgBox[0]).attr("src");
                    $(imgBox[1]).attr("src","../img/add_photo.jpg");
                    $(imgBox[1]).css('display','block');
                }else if($(imgBox[1]).attr('src')=='../img/add_photo.jpg'){
                    $(imgBox[1]).attr('src',img2);
                    imgs2=imgs2+","+$(imgBox[1]).attr("src");
                    $(imgBox[2]).attr("src","../img/add_photo.jpg");
                    $(imgBox[2]).css('display','block');
                }else if($(imgBox[2]).attr('src')=='../img/add_photo.jpg'){
                    $(imgBox[2]).attr('src',img2);
                    imgs2=imgs2+","+$(imgBox[2]).attr("src");
                    $(imgBox[3]).attr("src","../img/add_photo.jpg");
                    $(imgBox[3]).css('display','block');
                }else if($(imgBox[3]).attr('src')=='../img/add_photo.jpg'){
                    $(imgBox[3]).attr('src',img2);
                    imgs2=imgs2+","+$(imgBox[4]).attr("src");
                    $(imgBox[4]).attr("src","../img/add_photo.jpg");
                    $(imgBox[4]).css('display','block');
                }else if($(imgBox[4]).attr("src")==""||$(imgBox[4]).attr('src')=='../img/add_photo.jpg'){
                    $(imgBox[4]).attr('src',img2);
                    imgs2=imgs2+","+$(imgBox[4]).attr("src");
                }
                $('#'+a+'').parent().parent().attr('goodsImg',img2);

            },
            error:function(data){
                IMG2--;
                mui.alert('上传失败');

            }
        })
    }
}

